﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebMaster_ManagementProfile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadPage();
    }

    #region load page

    private void LoadPage()
    {
        string id = "";
        if (Request.Params[GlobalClassBE.MyUrlParameter.ObjectIDName] != null)
            id = Request.Params[GlobalClassBE.MyUrlParameter.ObjectIDName].ToString();

        GssDO.DOLogon DO = getByID(id);

        if (DO != null && DO.IsDeleted == 0)
        {
            pnlAddEdit.Visible = true;
            LoadInput(id);
            GlobalClassBE.MyMessage.MgsCurrentTitle = GlobalClassBE.PageName.ManagementProfileTitle;
        }
        else
        {
            Response.Redirect(@"Default.aspx", false);
        }
    }

    private void LoadInput(string id)
    {
        ShowTextForInput();
        if (!Page.IsPostBack)
        {
            GlobalClassBE.MyMessage.MgsCurrentMessages = "";

            ShowToEdit(id);
        }
    }

    #endregion load page

    #region show text

    private void ShowTextForInput()
    {
        lblFullname.Text = GlobalClassBE.LabelText.Fullname;
        lblUsername.Text = GlobalClassBE.LabelText.Username;
        lblEmail.Text = GlobalClassBE.LabelText.Email;

        reqFormatEmail.ErrorMessage = GlobalClassBE.MyMessage.InvalidEmailAddress;
        reqField.Text = GlobalClassBE.MyMessage.MgsRequiredField;

        btnSave.Text = GlobalClassBE.ButtonText.BtnSave;
        btnCancel.Text = GlobalClassBE.ButtonText.BtnCancel;
    }

    #endregion show text

    #region ShowAll

    private void ShowToEdit(string id)
    {
        try
        {
            GssDO.DOLogon DO = getByID(id);
            GssDO.DOMember memDO = getMemberByID(id);

            if (DO != null)
            {
                lblName.Text = DO.Username.ToLower();
                txtFullname.Text = memDO.Fullname;
                txtEmail.Text = memDO.Email;

                lblTempID.Text = id;
                lblActionName.Text = GlobalClassBE.ActionText.EditPage;
                ShowButton(false);
                GlobalClassBE.MyMessage.MgsCurrentMessages = "";
            }
            else
            {
                GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsEdit_Selected;
                CancelPage();
            }
        }
        catch (Exception exc)
        {
            string mess = exc.Message;
        }
    }
    private GssDO.DOLogon getByID(string id)
    {
        GssDO.DOLogon DO = null;
        try
        {
            GssBL.BLLogon BL = new GssBL.BLLogon();
            DO = BL.getLogonByID(id);
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            DO = null;
        }
        return DO;
    }
    private GssDO.DOMember getMemberByID(string id)
    {
        GssDO.DOMember DO = null;
        try
        {
            GssBL.BLMember BL = new GssBL.BLMember();
            DO = BL.getMemberByLogonId(id);
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            DO = null;
        }
        return DO;
    }
    /// <summary>
    /// show button on form (Parameter: true: show to add, false: show to edit)
    /// </summary>
    /// <param name="pFlag">type bool: true: show to add; false: show to edit</param>
    private void ShowButton(bool pFlag)
    {
        btnSave.Visible = true;
        btnCancel.Visible = true;
    }
    private void CancelPage()
    {
        Response.Redirect(@"Default.aspx", false);
    }
    private void SavePage(bool pFlag)
    {
        lblMessage.Text = "";
        if (validateForm())
        {
            string sID = lblTempID.Text.ToString();
            if (sID != "")
                SaveEdit(sID);
            else
            {
                GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsEdit_Selected;
                CancelPage();
            }
        }
    }
    private void SaveEdit(string id)
    {
        try
        {
            GssDO.DOMember memDO = fillDOMember(id);
            GssBL.BLMember BL = new GssBL.BLMember();
            BL.Update(memDO);
            GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsEdit_Success;
            CancelPage();
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            lblMessage.Text = GlobalClassBE.MyMessage.MgsEdit_UnSuccess;
        }
    }

    private bool validateByLabel()
    {
        bool result = true;
        try
        {
            result = true;
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
        return result;
    }
    private bool validateForm()
    {
        bool result = true;
        try
        {
            if (!reqFullname.IsValid)
                result = false;
            if (!reqEmail.IsValid)
                result = false;
            if (!reqFormatEmail.IsValid)
                result = false;

            if (result)
            {
                result = validateByLabel();
            }
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
        return result;
    }

    private GssDO.DOMember fillDOMember(string id)
    {
        GssDO.DOMember DO = getMemberByID(id);
        if (DO != null)
        {
            DO.Email = txtEmail.Text.Trim();
            DO.Fullname = txtFullname.Text.Trim();
        }
        return DO;
    }

    #endregion ShowAll

    /***** Add Edit ******/
    protected void btnSave_Click(object sender, EventArgs e)
    {
        SavePage(false);
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        CancelPage();
    }
}
